<?php

/* @version $Id: utils.php,v 1.5 2008-02-14 20:41:46 oleg Exp $ */

/**
 * Get current time.
 *
 * return real
 */
function timer_get_time()
{
    $mtime = explode(' ', microtime() );
    $mtime = $mtime[1] + $mtime[0];
    return $mtime;
}

/**
 * Start timer.
 *
 * @return bool TRUE
 */
function timer_start()
{
    global $timestart;
    $timestart = timer_get_time();
    return true;
}

/**
 * Stop timer.
 *
 * @param $precision int number precision
 *
 * @return real work time timer
 */
function timer_stop($precision = 4)
{
    global $timestart, $timeend;
    $timeend = timer_get_time();
    $timetotal = $timeend-$timestart;

    return number_format($timetotal, $precision);
}

// Start timer.
timer_start();

/**
 * Strip slashes.
 *
 * @param $el object
 */
function strips(&$el)
{
    if (is_array($el))
        foreach($el as $k=>$v)
            strips($el[$k]);
    else
        $el = stripslashes($el);
}

// Strip slashes for main global variables.
if (get_magic_quotes_gpc())
{
    strips($_GET);
    strips($_POST);
    strips($_COOKIE);
    strips($_REQUEST);
    strips($_SESSION);
    if (isset($_SERVER['PHP_AUTH_USER']))
        strips($_SERVER['PHP_AUTH_USER']);
    if (isset($_SERVER['PHP_AUTH_PW']))
        strips($_SERVER['PHP_AUTH_PW']);
}

function get_utc_time()
{
    return gmdate('Y-m-d H:i:s', time());
}

/**
 * Get translation text by translation name
 *
 * @param $name string translation name
 *
 * @return string translation text
 */
function text($name)
{
    global $WEB_APP;

    return (isset($WEB_APP['text'][$name]) ?
            $WEB_APP['text'][$name] : $name);
}

/**
 * Load translations.
 *
 * Load english translations. Config language translations. Result in
 * $WEB_APP['text'].
 */
function load_translations()
{
    global $WEB_APP;

    // Load translations.
    $english_language_id = get_language_id('English');
    if ($english_language_id == 0)
    {
        echo 'Install english translations.';
        exit();
    }
    $text = array();
    if ($english_language_id != $WEB_APP['settings']['language_id'])
    {
        $english_translations = db_extract(DB_TABLE_TRANSLATION,
            'language_id = '.$english_language_id);

        foreach($english_translations as $translation)
        {
            $text[$translation['name']] = $translation['text'];
        }
    }

    $translations = db_extract(DB_TABLE_TRANSLATION,
        'language_id = '.$WEB_APP['settings']['language_id']);

    foreach($translations as $translation)
    {
        $text[$translation['name']] = $translation['text'];
    }

    $WEB_APP['text'] = $text;
}

/**
 * Get value of array by parameter. 
 * If parameter does not exist, return default value.
 *
 * @param $array array
 * @param $param parameter name
 * @param $default default value
 *
 * @return value
 */
function get_value_array($array, $param, $default = '')
{
    return (isset($array[$param]) ? $array[$param] : $default);
}

/**
 * Create random password.
 *
 * @return string random password
 */
function create_password()
{
    $count = rand(3, 5)+4;
    $password = '';
    for ($i = 0; $i < $count; $i++)
    {
        $tmp = rand(0, 1);
        if ($tmp === 0)
        {
            $c = chr(rand(0, ord('z') - 
                ord('a')) + ord('a'));
        }
        else
        {
            $c = chr(ord('0') + rand(0, 9));
        }

        $password .= $c;
    }
    return $password;
}

define ('S_PHP_VERSION', 1);
define ('S_WRITABLE', 2);
define ('S_UNWRITABLE', 3);
define ('S_MYSQL_VERSION', 4);

function echo_status($bool, $status_const = 0, $version = 0)
{
    if ($status_const == 0)
    {
        if ($bool)
        {
            return "<font color=green><strong>Ok</strong></font>";
        }

        return  "<font color=red><strong>Error</strong></font>";
    }

    if ($status_const == S_PHP_VERSION)
    {
        if ($bool)
        {
            return "<font color=green><strong>Ok (ver " . PHP_VERSION . ")</strong></font>";
        }

        return  "<font color=red><strong>Error (ver 5.0 min)</strong></font>";
    }

    if ($status_const == S_WRITABLE)
    {
        if ($bool)
        {
            return "<font color=green><strong>Ok</strong></font>";
        }

        return  "<font color=red><strong>Error: Not Writable</strong></font>";
    }

    if ($status_const == S_UNWRITABLE)
    {
        if ($bool)
        {
            return "<font color=green><strong>Ok</strong></font>";
        }

        return  "<font color=red><strong>Error: Writable</strong></font>";
    }

    if ($status_const == S_MYSQL_VERSION)
    {
        if ($bool)
        {
            return "<font color=green><strong>Ok (ver " . $version . ")</strong></font>";
        }

        return  "<font color=red><strong>Error (ver ".CFG_MIN_MYSQL_VERSION." min)</strong></font>";
    }
}

function status_report($mysql_version = 0)
{
    $iconv = function_exists("iconv");
    $mysql_database = function_exists("mysql_connect");
    $gd = function_exists("gd_info");
    $mbstring = function_exists("mb_eregi");

    $writable_config =  !is_writable("system/config_inc.php");
    $path = dirname(__FILE__).'/../themes/sunrav/templates_c';
	// Check is writable folder.
	$is_writable = TRUE;
	$file = $path.DIRECTORY_SEPARATOR.uniqid(mt_rand()).'.tmp';
	if (!($f = @fopen($file, 'w+')))
	{
		$is_writable = FALSE;
	}
	else
	{
		$is_writable = TRUE;
		fclose($f);
		unlink($file);
	}
    $writable_templates_c_folder = $is_writable && is_writable($path);
    $xml_support = function_exists("xml_parser_create");
    $ver = explode( '.', PHP_VERSION );
    $php_version = $ver[0] . $ver[1] . $ver[2];
    $register_globals = (bool) ini_get('register_globals');
    $magic_quotes_gpc = (bool) ini_get('magic_quotes_gpc');
    $magic_quotes_runtime = (bool) ini_get('magic_quotes_runtime');
    $session_auto_start = (bool) ini_get('session.auto_start');
    $session_use_cookies = (bool) ini_get('session.use_cookies');
    $session_use_trans_sid = (bool) ini_get('session.use_trans_sid');
    $mbstring_encoding_translation = (bool) ini_get('mbstring.encoding_translation');
    $mbstring_internal_encoding = ini_get('mbstring.internal_encoding');
    $mbstring_http_input = ini_get('mbstring.http_input');
    $mbstring_http_output = ini_get('mbstring.http_output');

    $php_version_status = echo_status($php_version >= 500, S_PHP_VERSION);
    $iconv_status = echo_status($iconv);
    $mysql_database_status = echo_status($mysql_database);
    if ($mysql_database && ($mysql_version != 0))
    {
        $check_db_version = check_db_version($mysql_version);
        $mysql_database_status = echo_status($check_db_version, S_MYSQL_VERSION, $mysql_version);
        $mysql_database = $mysql_database && $check_db_version;
    }
    $gd_status = echo_status($gd);
    $mbstring_status = echo_status($mbstring);
    $xml_support_status = echo_status($xml_support);

    $writable_config_status = echo_status($writable_config, S_UNWRITABLE);

    $writable_templates_c_folder_status =  echo_status($writable_templates_c_folder, S_WRITABLE);
    $register_globals_status = echo_status(!$register_globals);
    $magic_quotes_gpc_status = echo_status(!$magic_quotes_gpc);
    $magic_quotes_runtime_status = echo_status(!$magic_quotes_runtime);
    $session_auto_start_status = echo_status(!$session_auto_start);
    $session_use_cookies_status = echo_status($session_use_cookies);
    $session_use_trans_sid_status = echo_status(!$session_use_trans_sid);
    $mbstring_encoding_translation_status = echo_status(!$mbstring_encoding_translation);
    $mbstring_internal_encoding_status = echo_status($mbstring_internal_encoding == "UTF-8");
    $mbstring_http_input_status = echo_status($mbstring_http_input == "UTF-8");
    $mbstring_http_output_status = echo_status($mbstring_http_output == "UTF-8");

    $disabled = !(($php_version >= 500) && $iconv &&
            $mysql_database && $gd && $mbstring &&
            $writable_config &&
            $writable_templates_c_folder &&
            $xml_support &&
            (!$register_globals) && (!$magic_quotes_gpc) &&
            (!$magic_quotes_runtime) &&
            (!$session_auto_start) && $session_use_cookies &&
            (!$session_use_trans_sid) &&
            (!$mbstring_encoding_translation) &&
            $mbstring_internal_encoding && $mbstring_http_input
            && $mbstring_http_output);

    if ($disabled)
    {
        //header("Content-Type: text/plain; charset=utf-8");
        ?>
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
        <title>Status Report</title>
        </head>
        <body>
        <table width = "100%" border = "0" cellspacing = "0" cellpadding = "0">
            <tr valign = "top">

                <td class = "col" colspan="2"><strong>Status Report</strong><br><br></td>

            </tr>
            <tr>

                <td class="namecol" width="70%">PHP Version</td>
                <td><?php echo $php_version_status;?></td>
            </tr>
            <tr>
                <td class="namecol" width="30%">MySQL Support</td>
                <td><?php echo $mysql_database_status;?></td>
            </tr>
            <tr>
                <td class="namecol">Iconv Module</td>
                <td><?php echo $iconv_status;?></td>
            </tr>
            <tr>
                <td class="namecol">MB Strings Module</td>
                <td><?php echo $mbstring_status;?></td>
            </tr>
            <tr>
                <td class="namecol">GD Module</td>
                <td><?php echo $gd_status;?></td>
            </tr>
            <tr>
                <td class="namecol">XML Support</td>
                <td><?php echo $xml_support_status;?></td>
            </tr>
            <tr>
                <td class="namecol">Unwritable Configuration File (system/config_inc.php)</td>
                <td><?php echo $writable_config_status;?></td>
            </tr>
            <tr>
                <td class="namecol">Writable templates_c folder (themes/sunrav/templates_c)</td>
                <td><?php echo $writable_templates_c_folder_status;?></td>
            </tr>

            <tr>

                <td class="namecol">register_globals = off</td>
                <td><?php echo $register_globals_status; ?></td>

            </tr>
            <tr>
                <td class="namecol">magic_quotes_gpc = off</td>
                <td><?php echo $magic_quotes_gpc_status;?></td>
            </tr>
            <tr>
                <td class="namecol">magic_quotes_runtime = off</td>
                <td><?php echo $magic_quotes_runtime_status;?></td>
            </tr>
            <tr>
                <td class="namecol">session.auto_start = off</td>
                <td><?php echo $session_auto_start_status;?></td>
            </tr>
            <tr>
                <td class="namecol">session.use_cookies = on</td>
                <td><?php echo $session_use_cookies_status;?></td>
            </tr>
            <tr>
                <td class="namecol">session.use_trans_sid = off</td>
                <td><?php echo $session_use_trans_sid_status;?></td>
            </tr>
            <tr>
                <td class="namecol">mbstring.encoding_translation = off</td>
                <td><?php echo $mbstring_encoding_translation_status;?></td>
            </tr>
            <tr class="line2">
                <td class="namecol">mbstring.internal_encoding = UTF-8</td>
                <td><?php echo $mbstring_internal_encoding_status;?></td>
            </tr>
            <tr>
                <td class="namecol">mbstring.http_input = UTF-8</td>
                <td><?php echo $mbstring_http_input_status;?></td>
            </tr>
            <tr>
                <td class="namecol">mbstring.http_output = UTF-8</td>
                <td><?php echo $mbstring_http_output_status;?></td>
            </tr>

        </table>
        </body>
        </html>

        <?php
        exit();
    }
}

